/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JGraphStruct.java
 *
 * Created on Jan 19, 2010, 10:57:13 AM
 */
package dae.fxcreator.node.graph;

import dae.fxcreator.io.FXSingleton;
import dae.fxcreator.node.ShaderField;
import dae.fxcreator.node.ShaderStruct;
import dae.fxcreator.node.StructListener;
import dae.fxcreator.node.gui.GraphFont;
import dae.fxcreator.node.gui.GraphGradient;
import java.awt.Color;


import java.awt.LinearGradientPaint;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.HashMap;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import org.jdesktop.swingx.border.MatteBorderExt;
import org.jdesktop.swingx.painter.MattePainter;

/**
 * This component displays the content of an IONode.
 * @author Koen
 */
public class JGraphStruct extends BoxPanel implements ComponentListener, StructListener {

    private ShaderStruct model;
    private HashMap<String, JField> fieldMap = new HashMap<String, JField>();

    /** Creates new form JGraphStruct */
    public JGraphStruct() {
        super(BoxLayout.Y_AXIS);
        initComponents();
        this.addComponentListener(this);
        MatteBorderExt ext = new MatteBorderExt(2, 2, 2, 2, Color.white);
        setBorder(ext);
    }

    /**
     * Called when the component is removed from the panel.
     */
    @Override
    public void removeNotify(){
        super.removeNotify();
        if (model != null)
            model.removeStructListener(this);
    }

    /**
     * Sets the ShaderStruct to show in this JGraphStruct object.
     * @param struct the struct to show.
     */
    public void setModel(ShaderStruct struct) {
        if (struct == null) {
            return;
        }
        if (struct != model) {
            if (model != null) {
                model.removeStructListener(this);
            }
            this.model = struct;
            model.addStructListener(this);

            this.removeAll();
            GraphFont gf = FXSingleton.getSingleton().getFXSettings().getFont("structtitle");
            JLabel lblStructName = new JLabel(struct.getId() + " struct");
            lblStructName.setFont(gf.getFont());
            lblStructName.setForeground(gf.getColor());
            lblStructName.setAlignmentX(0.0f);
            add(lblStructName);

            for (ShaderField field : struct.getFields()) {
               
                JField fieldcomp = new JField(field);
                fieldMap.put(field.getName(), fieldcomp);
                add(fieldcomp);
            }
            validate();
            setSize(this.getMinimumSize());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setLayout(new java.awt.GridBagLayout());
    }// </editor-fold>//GEN-END:initComponents

    public void componentResized(ComponentEvent e) {
        GraphGradient gg = FXSingleton.getSingleton().getFXSettings().getGradient("struct");
        /*
        //LinearGradientPaint gradientPaint =
                new LinearGradientPaint(0.0f, 0.0f, 0.0f, getHeight(),
                new float[]{0.0f, 1.0f}, new Color[]{gg.getC1(), gg.getC2()});
        MattePainter paint = new MattePainter(gradientPaint);
        this.setBackgroundPainter(paint);
         * */
    }

    public void componentMoved(ComponentEvent e) {
    }

    public void componentShown(ComponentEvent e) {
    }

    public void componentHidden(ComponentEvent e) {
    }

    public void structFieldUpdated(ShaderStruct struct, ShaderField field, ShaderField oldValue) {
        JField fieldcomp = fieldMap.get(oldValue.getName());
        if (fieldcomp != null) {
            fieldcomp.syncWithModel();
            repaint();
        }
    }

    public void structFieldInserted(ShaderStruct struct, ShaderField field) {
        JField fieldcomp = new JField(field);
        this.add(fieldcomp);
    }

    public void structFieldRemoved(ShaderStruct struct, ShaderField field) {
    }

    public void structIdChanged(ShaderStruct struct) {
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
